#include<SDL.h>
#include "Game.h"

Game * g_game = nullptr;
const int FPS = 60;
const int DELAY_TIME = 1000.0f/FPS;

int main(int argc, char ** argv){
    uint32_t frameStart, frameTime;
//    g_game = new Game();
    if (!TheGame::Instance()->init("sdl game develop",
                                100, 100,
                                640, 480,
                                false)){
        std::cout << "game init failure - " << SDL_GetError() << std::endl;
        return -1;
    }
//    g_game->init("my game", 100, 100, 640, 480, 0);
#if 0
    while(g_game->is_running()){
        g_game->handleEvents();
        g_game->update();
        g_game->render();
    }
#endif

    std::cout << "game init success!" << std::endl;
    while (TheGame::Instance()->is_running()){
        frameStart = SDL_GetTicks();
        TheGame::Instance()->handleEvents();
        TheGame::Instance()->update();
        TheGame::Instance()->render();
        frameTime = SDL_GetTicks() - frameStart;

        if (frameTime < DELAY_TIME){
            SDL_Delay((int)(DELAY_TIME - frameTime));
        }

//        SDL_Delay(10);
    }

//    g_game->clean();
    TheGame::Instance()->clean();

    return 0;
}



















#if 0
 SDL_Window* g_pWindow = 0;
 SDL_Renderer* g_pRenderer = 0;
 bool g_bRunning = false;


 void update(){}
 void handleEvents(){}
 void clean(){}

 //init
 void init(){
   // initialize SDL
  if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
  {
    // if succeeded create our window
    g_pWindow = SDL_CreateWindow("sdl game development", 
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        640, 480, 
        SDL_WINDOW_SHOWN);
    // if the window creation succeeded create our renderer
    if(g_pWindow != 0)
    {
    }
      g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
  }
  else
  {
    return; // sdl could not initialize
  }
   
 }

 //render
 void render(){
  // everything succeeded lets draw the window
  // set to black // This function expects Red, Green, Blue and 
  //  Alpha as color values
  SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255);
  // clear the window to black
  SDL_RenderClear(g_pRenderer);
  // show the window Getting Started with SDL
  SDL_RenderPresent(g_pRenderer);
 }


 int main(int argc, char* args[])
 {
    init();
    g_bRunning = true;
    while(g_bRunning){
        //input
        //todo
         handleEvents();
         update();
        //do physical

        //todo
        
        render();
        SDL_Delay(5000);
        g_bRunning = false;
    }
   // set a delay before quitting
  SDL_Delay(5000);
  // clean up SDL
  SDL_Quit();
  return 0;
 }
#endif
